package com.fish.tes;

import com.fish.io.Resources;
import com.fish.mapper.UserMapper;
import com.fish.pojo.User;
import com.fish.sqlSession.SqlSessionFactory;
import com.fish.sqlSession.SqlSessionFactoryBuilder;
import com.fish.sqlSession.Sqlsession;
import org.dom4j.DocumentException;
import org.junit.Before;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;

/**
 * 用户测试类
 */
public class IpersistentTest {

    private Sqlsession sqlsession;

    @Before
    public void buildSqlSession() throws PropertyVetoException, DocumentException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfigration.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlsession = sqlSessionFactory.openSession();
    }

    @Test
    public void test() throws Exception {

//        List<User> userList = sqlsession.selectList("com.fish.mapper.selectList");
//        userList.forEach(user ->{
//            System.out.println(user);
//        });

        User user = new User();
        user.setId(1);
        User user1 = sqlsession.selectOne("com.fish.mapper.selectOne",user);
        System.out.println(user1);
    }

    @Test
    public void testSelectList() throws Exception {
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        List<User> userList = mapper.selectList();
        userList.forEach(user -> {
            System.out.println(user);
        });
    }

    @Test
    public void testSelectOne() throws Exception {
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        User user = new User();
        user.setId(1);
        User user1 = mapper.selectOne(user);
        System.out.println(user1);
    }

    @Test
    public void testInsertOne() throws Exception {
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        User user = new User();
        user.setId(6);
        user.setUsername("aaa");
        user.setPassword("aaa");
        mapper.insertOne(user);
    }

    @Test
    public void testUpdateOne() throws Exception {
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        User user = new User();
        user.setId(6);
        user.setUsername("ccc");
        user.setPassword("ccc");
        mapper.updateOne(user);
    }

    @Test
    public void testDeleteById() throws Exception {
        UserMapper mapper = sqlsession.getMapper(UserMapper.class);
        mapper.deleteById(5);
    }
}
